home *** CD-ROM | disk | FTP | other *** search
- Path: ccshst05.cs.uoguelph.ca!ccshst01!thay
- From: thay@uoguelph.ca (Toby K Hay)
- Newsgroups: comp.lang.c
- Subject: Re: segmentation fault
- Date: 4 Apr 1996 12:33:57 GMT
- Organization: University of Guelph
- Message-ID: <4k0fjl$ir@ccshst05.cs.uoguelph.ca>
- References: <31636B02.55C6@cts.com>
- NNTP-Posting-Host: ccshst01.cs.uoguelph.ca
- X-Newsreader: TIN [version 1.2 PL2]
-
- Frank Kragh (kragh@cts.com) wrote:
- : When running a program, (which didn't yield any errors during compiling),
- : I get a "Segmentation fault". Anybody know what this is and/or what
- : might be wrong with my program?
-
- I posted this same question here recently and received plenty of
- good advice which I'll summarize very briefly. A segmentation fault
- while running a program usually (always?) means that your program is
- accessing memory that wasn't allocated to it. It doesn't happen on a DOS
- machine becuase the OS doesn't do that check - the program can write
- wherever it likes, maybe overwriting something vital and crashing the
- machine, or maybe not. On UNIX the OS prevents it and halts the program
- with a segmentation fault.
- Most people who replied to my question suggested:
- 1) after a call to malloc() check that the returned pointer is not NULL
- so that the program doesn't ever try to access memory via a null pointer.
- 2) check that all calls to free() had a valid pointer - no trying to
- free() a pointer without a previous call to malloc() or that had already
- been free()d.
- 3) check that the program wasn't reading or writing arrays beyond their
- declared bounds (e.g. trying to access array elements RA[-1] or RA[7] on
- an array declared as int RA[7] whose elements will be numbered 0 to 6).
- (This was the bug in my program.)
- Toby Hay thay@uoguelph.ca
-
-
-